package com.springboot.demo.service.impl;

import com.springboot.demo.entity.Menu;
import com.springboot.demo.entity.User;
import com.springboot.demo.mapper.MenuMapper;
import com.springboot.demo.mapper.UserMapper;
import com.springboot.demo.service.MenuService;
import com.springboot.demo.service.UserService;
import com.springboot.demo.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author wubc683
 * @desc
 * @date 2021-08-31 16:25
 */
@Service
@Slf4j
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuMapper menuMapper;
    @Autowired
    RedisUtils redisUtils;

    @Override
    public List<Menu> getAllMenus() {
        List<Menu> allMenus = menuMapper.getAllMenus();
        return allMenus;
    }

    @Override
    public List<Long> getMenuAllRoles(String url, String method) {
        Set<Object> objects = redisUtils.sGet("menu::url::" + url + "::" + "method::" + method);
        List<Long> menuAllRoles = new ArrayList<>();
        if (objects != null && !objects.isEmpty()) {
            Object[] objects1 = objects.toArray();
            for (Object o : objects1) {
                menuAllRoles.add(Long.valueOf(o.toString()));
            }
        }
//        log.info("before sSet");
        menuAllRoles = menuMapper.getMenuAllRolesByUrlAndMethod(url, method);
        if (menuAllRoles != null && !menuAllRoles.isEmpty()) {
            redisUtils.sSet("menu::url::" + url + "::" + "method::" + method, menuAllRoles.toArray());
        }
//        log.info("after sSet");
        return menuAllRoles;
    }

}
